package com.ias.agdis_r.activity.setting;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseKeyActivity;
import com.ias.agdis_r.activity.MainActivity;

/**
 * Created by qibo.yu on 2015/9/11.
 */
public class RingerVolumesSettingsActivity extends BaseKeyActivity implements SeekBar.OnSeekBarChangeListener {

    private SeekBar seekBarNotification;
    private SeekBar seekBarMedia;
    private SeekBar seekBarAlarm;

    private AudioManager audioManager;

    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ringer_volume);

        initactionBar();
        setTitle(R.string.RingerVolumesSettingsActivity);

        this.audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);



        seekBarNotification = (SeekBar) findViewById(R.id.seek_notification);
        seekBarMedia = (SeekBar) findViewById(R.id.seek_media);
        seekBarAlarm = (SeekBar) findViewById(R.id.seek_alarm);

        seekBarNotification.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));
        seekBarMedia.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        seekBarAlarm.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));

        seekBarNotification.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION));
        seekBarMedia.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
        seekBarAlarm.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));

        seekBarNotification.setOnSeekBarChangeListener(this);
        seekBarMedia.setOnSeekBarChangeListener(this);
        seekBarAlarm.setOnSeekBarChangeListener(this);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Log.d("c_voice", "progress:" + progress);

        switch (seekBar.getId()){
            case R.id.seek_notification:
                audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, progress, AudioManager.FLAG_PLAY_SOUND);
                break;
            case R.id.seek_media:
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_PLAY_SOUND);
                break;
            case  R.id.seek_alarm:
                audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, AudioManager.FLAG_PLAY_SOUND);
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void setTitle(CharSequence title) {
        this.title.setText(title);
        super.setTitle(title);
    }

    @Override
    public void setTitle(int titleId) {
        this.setTitle(getString(titleId));
    }

    @SuppressLint("InflateParams")
    private void initactionBar() {
        final Activity context = this;

        ActionBar actionBar = getActionBar();

        actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_settings, null));
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);

        this.title = (TextView) findViewById(R.id.txt_title);

        ImageView back = (ImageView) findViewById(R.id.actionbar_settings_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }
}
